package 每日一题;

public class 奇偶交换 {
    public static int[] evenOddSwap(int[] nums) {
        int n = nums.length;
        boolean[] st = new boolean[n];
        // l 指向奇数,r 指向偶数
        int l = 0,r = 0;
        while(l < n && r < n){
            while (st[r])
                r++;
            while (st[l])
                l++;
            // 让l指向奇数
            while (l < n  && !st[l] && nums[l] % 2 == 0)
                l++;
            // 让r指向偶数
            while(r < n  && !st[r] && nums[r] % 2 != 0)
                r++;
            if(l >= n || r >= n)
                break;
            int temp = nums[l];
            nums[l] = nums[r];
            nums[r] = temp;
            st[l]=st[r]=true;
            l++;
            r++;
        }
        return nums;
    }

    public static void main(String[] args) {
        int[] nums = {11,55,100,200,300,7};
        int[] ans = evenOddSwap(nums);
        for(int i = 0;i < ans.length;i++){
            System.out.print(nums[i] + " ");
        }
    }

}
